<template>
  <div class="goods-detail">
    <div class="container" v-if="goods && !loading">
      <!-- 面包屑 -->
      <xtx-bread>
        <xtx-bread-item to="/">首页</xtx-bread-item>
        <xtx-bread-item :to="'/category/' + goods.categories[1].id">{{
          goods.categories[1].name
        }}</xtx-bread-item>
        <xtx-bread-item :to="'/category/sub/' + goods.categories[0].id">{{
          goods.categories[0].name
        }}</xtx-bread-item>
        <xtx-bread-item>{{ goods.name }}</xtx-bread-item>
      </xtx-bread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 商品信息左边 -->
        <div class="goods-info-left">
          <goodsImage :images="goods.mainPictures"></goodsImage>
          <goodsSales></goodsSales>
        </div>
        <!-- 商品信息右边 -->
        <div class="goods-info-right">
          <!-- 名字组件 -->
          <goodsName :goods="goods"></goodsName>
          <!-- 规格组件 -->
          <goodsSku :goods="goods" @change="changeSku"></goodsSku>
          <!-- 商品数量组件 -->
          <xtx-number
            :label="label"
            v-model="count"
            :max="goods.inventory"
          ></xtx-number>
          <!-- 加入购物车按钮 -->
          <xtx-button
            type="primary"
            @click="addToCart()"
            style="margin-top: 20px"
            >加入购物车</xtx-button
          >
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goods.id" />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品 + 评价 -->
          <goodsTabs></goodsTabs>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <goodsWarn></goodsWarn>
          </div>
        </div>
        <!-- 24热榜 + 专题推荐 -->
        <div class="goods-aside">
          <goodsHot :goodsId="goods.id" :type="1"></goodsHot>
          <goodsHot :goodsId="goods.id" :type="2"></goodsHot>
        </div>
      </div>
    </div>
    <div v-else class="loading"></div>
  </div>
</template>

<script>
import { nextTick, ref, watch, provide } from 'vue'
import GoodsRelevant from './components/goods-relevant.vue'
import goodsImage from './components/goods-image.vue'
import goodsSales from './components/goods-sales.vue'
import goodsName from './components/goods-name.vue'
import goodsSku from './components/goods-sku.vue'
import goodsTabs from './components/goods-tabs.vue'
import goodsHot from './components/goods-hot.vue'
import goodsWarn from './components/goods-warn.vue'
import { message } from '../../components/library/message'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { findGoods } from '@/api/goods-detail'
export default {
  name: 'GoodsDetail',
  components: {
    GoodsRelevant,
    goodsImage,
    goodsSales,
    goodsName,
    goodsSku,
    goodsTabs,
    goodsHot,
    goodsWarn
  },
  setup() {
    const label = '数量'
    const route = useRoute()
    const goods = ref(null)
    // 加载中
    const loading = ref(false)
    // 选择的数量
    const count = ref(1)
    // 提供数据，子孙注入
    provide('goods', goods)
    // 注意：单页面应用路由变化，需要重新获取商品详情
    watch(
      () => route.params.id,
      async (id) => {
        // 如果不是商品详情页不处理
        if (route.path !== `/product/${id}`) return
        loading.value = true
        const { result } = await findGoods(id)
        // goods.value = null
        nextTick(() => {
          goods.value = result
          loading.value = false
        })
        count.value = 1
      },
      { immediate: true }
    )
    // sku改变时候触发
    const changeSku = (sku) => {
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
        // 选中的规格，加入购物车的时候要用到
        goods.value.attrsText = sku.attrsText
        // 存储当前skuid，加入购物车的时候要用到
        goods.value.currSkuId = sku.skuId
      } else {
        // 没有选择完整规格就重置为空
        goods.value.currSkuId = ''
      }
    }
    const store = useStore()
    // 加入购物车按钮
    const addToCart = () => {
      console.log(goods.value.currSkuId, 'skuid')
      // 判断是否选择了完整规格
      if (!goods.value.currSkuId) {
        return message({ type: 'warn', message: '请选择完整规格' })
      }
      // 库存不足提示
      if (count.value > goods.value.inventory) {
        return message({ type: 'warn', message: '库存不足了' })
      }
      // 约定本地存储信息内容 - 字段名可参考购物车列表接口的字段名
      const cartItem = {
        id: goods.value.id,
        name: goods.value.name,
        picture: goods.value.mainPictures[0],
        price: goods.value.price,
        skuId: goods.value.currSkuId,
        attrsText: goods.value.attrsText,
        nowPrice: goods.value.price,
        stock: goods.value.inventory,
        count: count.value,
        selected: true,
        isCollect: false,
        isEffective: true,
        discount: 1
      }
      // 添加到vuex仓库中，vuex数据做了本地存储同步，所以可以通过本地存储查看数据
      store.dispatch('cart/insertCart', cartItem).then(() => {
        message({ type: 'success', message: '加入购物车成功' })
      })
    }
    return { goods, changeSku, count, loading, addToCart, label }
  }
}
</script>
<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background-color: #fff;
  display: flex;
  .goods-info-left {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .goods-info-right {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
    position: relative;
    margin-left: 20px;
  }
}
// .goods-tabs {
//   min-height: 600px;
//   background-color: #fff;
// }
.goods-warn {
  min-height: 600px;
  background-color: #fff;
  margin-top: 20px;
  margin-bottom: 20px;
}
.loading {
  width: 1240px;
  min-height: 600px;
  background-color: #fff !important;
  margin: 70px auto 45px auto;
  background: url(~@/assets/images/loading.gif) no-repeat center;
}
</style>
